import 'package:fluro/fluro.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hxdj/common/base_page/base_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/my_common.dart';
import 'package:hxdj/common/my_constant.dart';
import 'package:hxdj/common/my_global.dart';
import 'package:hxdj/common/routers/navigator.dart';
import 'package:hxdj/login/login_page.dart';
import 'package:hxdj/message/rongcloud/rc_engine_manager.dart';

class CancelAccountViewModel extends BaseViewModel {
  bool isGetSmsCode = false;

  //注销账号的步骤
  int setup = 0;

  setSetup(int up) {
    setup = up;
    notifyListeners();
  }

  setIsGetSmsCode(bool getSms) {
    isGetSmsCode = getSms;
    notifyListeners();
  }

  //去请求服务器数据.获取验证码
  updateSmsVerificationCode(String mobile) async {
    Map<String, dynamic> parmaMap = {"mobile": mobile};
    // 去请求服务器数据.

    Map<String, dynamic> resultMap =
        await DioHttpManager().request(MyApi.getsms, method: DioHttpManager.post, data: parmaMap, isLoading: true);

    if (resultMap['code'] == 0) {
      EasyLoading.showToast("发送验证码成功");

      //开始倒计时
      setIsGetSmsCode(true);
    } else {
      EasyLoading.showToast("发送验证码失败");
      setIsGetSmsCode(false);
    }
  }

  // 取消账号 register_out
  Future<void> cancelAccount() async {
    setState(ViewState.loading);
    // 去请求服务器数据.
    Map<String, dynamic> resultMap =
        await DioHttpManager().request(MyApi.registerOut, method: DioHttpManager.post, isLoading: true);
    if (resultMap['code'] == 0) {
      EasyLoading.showToast("注销成功");
      CommonPlugin.sharedPreferences!.setBool(MyConstant.isLogin, false);

      await RCEngineManager().disconnectIM();
      CommonPlugin.sharedPreferences!.remove(MyConstant.userInfo);
      CommonPlugin.sharedPreferences!.remove(MyConstant.token);

      MyGlobalViewModel globalViewModel = MyGlobalViewModel.getGlobalViewModel(context);
      globalViewModel.setIndex(0);
      //跳转到主页面
      NavigatorUtils.pushParams(context, LoginPage.routeName,
          clearStack: true, replace: true, transition: TransitionType.fadeIn);
    } else {
      EasyLoading.showToast("注销失败");
    }
    // 更新页面.
    setState(ViewState.normal);
  }
}
